package com.aaie.gru.payproof.controller;


import com.aaie.gru.payproof.entity.GruSysPayProof;
import com.aaie.gru.payproof.entity.GruSysPayProofDto;
import com.aaie.gru.payproof.entity.GruSysPayProofVo;
import com.aaie.gru.payproof.service.IGruSysPayProofService;
import com.aaie.gru.response.ResMessage;
import com.aaie.gru.util.CommonUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvxing
 * @since 2019-08-09
 */
@Api(tags = "支付凭证模块")
@RestController
@RequestMapping("/payproof")
public class GruSysPayProofController {

    @Autowired
    IGruSysPayProofService gruSysPayProofService;

    @ApiOperation(value = "获取支付凭证列表", notes = "获取支付凭证列表")
    @PostMapping("/getGruSysPayProofList")
    public String getGruSysPayProofList(@RequestBody GruSysPayProofDto param) {
        try {
            IPage<GruSysPayProofVo> page = gruSysPayProofService.getGruSysPayProofVoList(param);
            return ResMessage.ok(page.getRecords(), param.getCurrent(), param.getSize(), page.getTotal());
        } catch (BeansException e) {
            e.printStackTrace();
            return ResMessage.error("/payproof/getGruSysPayProofList报错", 500);
        }

    }

    @ApiOperation(value = "添加支付凭证", notes = "添加支付凭证")
    @PostMapping("/addGruSysPayProof")
    public String addGruSysPayProof(@RequestBody GruSysPayProof gruSysPayProof, HttpSession session) {
        try {
            if (StringUtils.isNotEmpty(gruSysPayProof.getPayProofId())) {
                return ResMessage.error("新增的数据不能有主键id", 200);
            }
            // 学生id不能为空
            if (StringUtils.isEmpty(gruSysPayProof.getStuId())) {
                return ResMessage.error("学生id不能为空", 200);
            }
            // 支付凭证图片不能为空
            if (StringUtils.isEmpty(gruSysPayProof.getPayProofPhotoPath())) {
                return ResMessage.error("支付凭证图片不能为空", 200);
            }
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            String username = CommonUtil.getUser(session).getUserName();
            // 下方是通用代码
            gruSysPayProof.setCreateName(username);
            gruSysPayProof.setCreateTime(updateTime);
            gruSysPayProof.setCreateTimeSort(updateTimeSort);
            gruSysPayProof.setUpdateName(username);
            gruSysPayProof.setUpdateTime(updateTime);
            gruSysPayProof.setUpdateTimeSort(updateTimeSort);
            boolean save = gruSysPayProofService.save(gruSysPayProof);
            if (!save) {
                return ResMessage.error("数据插入失败", 200);
            }
            return ResMessage.ok(gruSysPayProof);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/payproof/addGruSysPayProof报错", 500);
        }
    }

    @ApiOperation(value = "更新支付凭证", notes = "更新支付凭证")
    @PostMapping("/editGruSysPayProof")
    public String editGruSysThemeFeedback(@RequestBody GruSysPayProof gruSysPayProof, HttpSession session) {
        try {
            if (StringUtils.isEmpty(gruSysPayProof.getPayProofId())) {
                return ResMessage.error("更新的数据必须有主键id", 200);
            }
            // 学生id不能为空
            if (StringUtils.isEmpty(gruSysPayProof.getStuId())) {
                return ResMessage.error("学生id不能为空", 200);
            }
            // 支付凭证图片不能为空
            if (StringUtils.isEmpty(gruSysPayProof.getPayProofPhotoPath())) {
                return ResMessage.error("支付凭证图片不能为空", 200);
            }
            gruSysPayProof.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysPayProof.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysPayProof.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            boolean b = gruSysPayProofService.updateById(gruSysPayProof);
            if (!b) {
                return ResMessage.error("数据更新失败", 200);
            }
            return ResMessage.ok(gruSysPayProof);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/payproof/editGruSysPayProof报错", 500);
        }

    }

    @ApiOperation(value = "按id获取支付凭证", notes = "按id获取支付凭证")
    @GetMapping("/getGruSysPayProof")
    public String getGruSysThemeFeedback(String payProofId) {
        try {
            if (StringUtils.isEmpty(payProofId)) {
                return ResMessage.error("获取数据必须有主键id", 200);
            }
            GruSysPayProof gruSysPayProof = gruSysPayProofService.getById(payProofId);
            if (gruSysPayProof == null) {
                return ResMessage.error("获取失败", 200);
            }
            return ResMessage.ok(gruSysPayProof);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/payproof/getGruSysPayProof报错", 500);
        }

    }

    @ApiOperation(value = "删除支付凭证", notes = "删除支付凭证")
    @PostMapping("/delGruSysPayProof")
    public String delGruSysThemeFeedback(@RequestBody GruSysPayProof gruSysPayProof) {
        try {
            if (StringUtils.isEmpty(gruSysPayProof.getPayProofId())) {
                return ResMessage.error("删除数据必须有主键id", 200);
            }
            boolean b = gruSysPayProofService.removeById(gruSysPayProof.getPayProofId());
            if (b) {
                return ResMessage.ok("删除数据成功");
            }
            return ResMessage.error("删除数据失败", 200);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/payproof/delGruSysPayProof报错", 500);
        }

    }

}
